package com.haojin.java;

import org.junit.Test;

import java.io.*;

/**
 * @author .29.
 * @create 2022-04-15 13:05
 */
/*
处理流之一：缓冲流的使用

1。缓冲流：
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferWriter

2.作用：提高流的读取与写入的速度
     能够提高效率的原因：内部提供了一个缓冲区
 */
public class BufferedTest {
    /*
    实现非文本文件的复制
     */
    @Test
    public void BufferedStreamTest() {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        BufferedInputStream bis = null;
        try {
            //1.造文件
            File srcFile = new File("p.JPG");
            File destFile = new File("p2.JPG");
            //2.造流
            //2.1造节点流
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);
            //2.2造缓冲流
            bos = new BufferedOutputStream(fos);
            bis = new BufferedInputStream(fis);

            //3.读取，写入操作
            byte[] buffer = new byte[5];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
//                bos.flush();//刷新缓冲区
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭流资源
            //要求：先关外层的流再关闭内层的流
            try {
                if (bis != null)
                    bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bos != null)
                    bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //说明：在关闭外层流的同时，内层流自动关闭
//            fis.close();
//            fos.close();
        }
    }

    //实现文件复制方法
    public void copyFileWithBuffered(String srcPath, String destPath) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        BufferedInputStream bis = null;
        try {
            //1.造文件
            File srcFile = new File(srcPath);
            File destFile = new File(destPath);
            //2.造流
            //2.1造节点流
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);
            //2.2造缓冲流
            bos = new BufferedOutputStream(fos);
            bis = new BufferedInputStream(fis);

            //3.读取，写入操作
            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭流资源
            //要求：先关外层的流再关闭内层的流
            try {
                if (bis != null)
                    bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bos != null)
                    bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //说明：在关闭外层流的同时，内层流自动关闭
//            fis.close();
//            fos.close();
        }
    }
    @Test
    public void testCopyFileWithBuffered(){
        long startTime = System.currentTimeMillis();

        String scrPath = "C:\\Users\\10575\\Desktop\\q1.mp4";
        String destPath = "C:\\Users\\10575\\Desktop\\q2.mp4";
        copyFileWithBuffered(scrPath,destPath);

        long endTime = System.currentTimeMillis();
        System.out.println("耗时："+(endTime-startTime));
    }

    /*
    使用BufferReader和BufferWriter实现文本文件的复制
     */
    @Test
    public void testBufferReaderBufferWriter()   {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            //
            br = new BufferedReader(new FileReader(new File("hi hi hi.txt")));
            bw = new BufferedWriter(new FileWriter(new File("hi hi.txt")));
            //方式一：使用char[]数组
//        char[] cbuf = new char[5];
//        int len;
//        while((len = br.read(cbuf)) != -1){
//            bw.write(cbuf,0,len);
//                bw.flush();
//        }
            //方式二：使用String
            String data;
            while((data = br.readLine()) != null){
                //方法一：
//            bw.write(data + "\n");//data中不包含换行符
                //方式二：
                bw.write(data);
                bw.newLine();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //
            try {
                if (br != null)
                    br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bw != null)
                    bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /*
    复制的方法
     */
public void copy(String srcPath,String destPath){
    BufferedReader br = null;
    BufferedWriter bw = null;
    try {
        //
        br = new BufferedReader(new FileReader(new File(srcPath)));
        bw = new BufferedWriter(new FileWriter(new File(destPath)));
        //方式一：使用char[]数组
//        char[] cbuf = new char[5];
//        int len;
//        while((len = br.read(cbuf)) != -1){
//            bw.write(cbuf,0,len);
//                bw.flush();
//        }
        //方式二：使用String
        String data;
        while((data = br.readLine()) != null){
            //方法一：
//            bw.write(data + "\n");//data中不包含换行符
            //方式二：
            bw.write(data);
            bw.newLine();
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        //
        try {
            if (br != null)
                br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if (bw != null)
                bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
@Test
    public void testCopy(){
    long startTime = System.currentTimeMillis();

    String scrPath = "hi hi.txt";
    String destPath = "hi.txt";
    copy(scrPath,destPath);

    long endTime = System.currentTimeMillis();
    System.out.println("耗时："+(endTime-startTime));
}
}
